package com.example.view.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;

public class WaveView extends View {

    private static final int DIS_CENTER = 12;
    private boolean isRunning = false;
    private ArrayList<Wave> wList;
    private int[] colors = {Color.RED, Color.YELLOW, Color.BLUE, Color.GREEN};

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            //刷新数据
            flushData();
            //刷新页面
            invalidate();
            //循环动画
            if (isRunning) {
                handler.sendEmptyMessageDelayed(0, 50);
            }
        }
    };

    public WaveView(Context context) {
        super(context);
    }

    public WaveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        wList = new ArrayList<>();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                int x = (int) event.getX();
                int y = (int) event.getY();
                addPoint(x, y);
                break;
            case MotionEvent.ACTION_UP:

                break;

        }
        return true;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < wList.size(); i++) {
            Wave wave = wList.get(i);
            canvas.drawCircle(wave.pointX, wave.pointY, wave.radius, wave.paint);
        }
    }

    /**
     * 添加波浪中心点
     *
     * @param x
     * @param y
     */
    private void addPoint(int x, int y) {
        if (wList.size() == 0) {
            addWave(x, y);
            //启动动画
            isRunning = true;
            handler.sendEmptyMessage(0);
        } else {
            Wave wave = wList.get(wList.size() - 1);
            if (Math.abs(wave.pointX - x) > DIS_CENTER || Math.abs(wave.pointY - y) > DIS_CENTER) {
                addWave(x, y);
            }
        }
    }

    /**
     * 添加一个波浪
     *
     * @param x
     * @param y
     */
    private void addWave(int x, int y) {
        Wave wave = new Wave();
        wave.pointX = x;
        wave.pointY = y;
        Paint paint = new Paint();
        paint.setColor(colors[(int) (Math.random() * 4)]);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        wave.paint = paint;
        wList.add(wave);
    }

    private void flushData() {
        for (int i = 0; i < wList.size(); i++) {
            Wave wave = wList.get(i);
            int alpha = wave.paint.getAlpha();
            //透明的为0从集合中删除
            if (alpha <= 0) {
                wList.remove(i);
//                i--;  //删除i 以后，i的值应该再减1 否则会漏掉一个对象，一般效果上看不出来。
                continue;
            }
            //降低透明度
            alpha -= 5;
            if (alpha <= 0) {
                alpha = 0;
            }
            wave.paint.setAlpha(alpha);
            //扩大波浪半径
            wave.radius += 8;
            //扩大波纹厚度
            wave.paint.setStrokeWidth(wave.radius / 3);
            //集合被清空，停止刷新动画
            if (wList.size() <= 0) {
                isRunning = false;
            }
        }
    }


    private class Wave {
        //圆心
        int pointX;
        int pointY;
        //画笔
        Paint paint;
        //半径
        int radius;
    }

}
